package com.yugao.fintech.draper.core.exception;

import com.yugao.fintech.draper.core.model.ErrorCode;

import java.text.MessageFormat;

public class BusinessException extends RuntimeException {

	private static final long serialVersionUID = 1L;

	private Integer code;

	private String message;

	public BusinessException(String message) {
		this.message = message;
	}

	public BusinessException(String message, Integer code) {
		this.message = message;
		this.code = code;
	}

	public BusinessException(String message, Throwable e) {
		super(message, e);
		this.message = message;
	}

	public BusinessException(ErrorCode errorCode) {
		this.message = errorCode.getMsg();
		this.code = errorCode.getCode();
	}

	/**
	 * 直接使用ResultTip构造,msgParam替换占位符{0}、{1}
	 */
	public BusinessException(ErrorCode errorCode, String msg, String... msgParam) {
		this.message = MessageFormat.format(msg, msgParam);
		this.code = errorCode.getCode();
	}

	/**
	 * 提供自定义返回消息的构造方法
	 */
	public BusinessException(ErrorCode errorCode, String msg) {
		this.message = msg;
		this.code = errorCode.getCode();
	}

	@Override
	public String getMessage() {
		return message;
	}

	public Integer getCode() {
		return code;
	}

}
